package com.campus.customer.controller;

import com.campus.common.bean.common.Resp;
import com.campus.customer.entity.Coupon;
import com.campus.customer.vo.UserInfoVo;
import com.campus.customer.bean.coupon.CouponQueryParam;
import com.campus.customer.service.CouponService;
import com.campus.customer.service.SessionService;
import com.campus.customer.vo.UserCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;
    
    @Autowired
    private SessionService sessionService;

    /**
     * 获取用户优惠券列表
     * @return 用户优惠券列表
     */
    @GetMapping("/list")
    public Resp<List<UserCouponVo>> getUserCoupons(CouponQueryParam param) {
        // 获取当前用户信息
        UserInfoVo userInfo = sessionService.getUserInfo();

        // 查询用户的优惠券列表
        List<UserCouponVo> coupons = couponService.getUserCouponList(userInfo.getId(), param);

        return Resp.success(coupons);
    }

    /**
     * 获取可领取的优惠券列表
     * @return 可领取的优惠券列表
     */
    @GetMapping("/receivable")
    public Resp<List<Coupon>> getReceivableCoupons() {
        // 查询可领取的优惠券列表
        List<Coupon> coupons = couponService.getReceivableCoupons();

        return Resp.success(coupons);
    }

    /**
     * 领取优惠券
     * @param couponId 优惠券ID
     * @return 领取结果
     */
    @PostMapping("/receive/{couponId}")
    public Resp<String> receiveCoupon(@PathVariable Integer couponId) {
        // 获取当前用户信息
        UserInfoVo userInfo = sessionService.getUserInfo();

        // 领取优惠券
        couponService.receiveCoupon(userInfo.getId(), couponId);

        return Resp.success("领取成功");
    }
}
